import { HashService } from '@app/hash';
import { PrismaService } from '@app/prisma';
import { SysUserEntity } from '@app/prisma/sys.user.entity/sys.user.entity';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { getCurrentTotal, GqlAuthGuard, GqlCurrentUser } from './auth.guard';
import { AuthService } from './auth.service';
import { ForbiddenError } from '@nestjs/apollo';
import { UserService } from 'src/system/user/user.service';

@Resolver()
export class AuthResolver {
  constructor(
    private readonly prisma: PrismaService,
    private readonly authService: AuthService,
    private readonly hash: HashService,
    private readonly userService: UserService,
  ) { }

  /**
   * 获取当前在线人数
   *
   * @returns 当前在线人数
   */
  @Query(() => Int)
  getCurrentTotal() {
    return getCurrentTotal()
  }

  @Query(() => SysUserEntity)
  @UseGuards(new GqlAuthGuard)
  viewer(
    @GqlCurrentUser() user: SysUserEntity
  ) {
    return user;
  }

  @Mutation(() => Boolean)
  async initAdmin(
    @Args("account") account: string,
    @Args("password") password: string,
    @Args("name") name: string,
    @Args("province", { nullable: true }) province?: string,
    @Args("city", { nullable: true }) city?: string,
    @Args("email", { nullable: true }) email?: string,
  ) {
    return this.authService.initAdmin(account, password, name, province, city, email)
  }

  @Query(() => Boolean)
  async isAdmin() {
    return (await this.prisma.sys_user.count() > 0)
  }

  /**
   * 账号密码登录
   */
  @Mutation(() => String)
  async auth(
    @Args("account") account: string,
    @Args("password") password: string
  ) {
    const find: SysUserEntity = await this.prisma.sys_user.findUnique({
      where: {
        account,
      }
    })
    if (!find) throw new ForbiddenError("找不到用户")
    if (!this.hash.verifyPassword(password, find.salt, find.password)) {
      throw new ForbiddenError("密码错误")
    }
    const user: SysUserEntity = await this.prisma.sys_user.update({
      where: {
        id: find.id
      },
      data: {
        loginId: this.hash.createUid()
      }
    })
    return this.authService.getToken(user).access_token
  }


  /**
   * 个人注册
   */
  @Mutation(() => String)
  async userRegister(
    @Args("account") account: string,
    @Args("password") password: string,
    @Args("name") name: string,
    @Args("province", { nullable: true }) province?: string,
    @Args("city", { nullable: true }) city?: string,
    @Args("email", { nullable: true }) email?: string,
  ) {
    const find: SysUserEntity = await this.prisma.sys_user.findUnique({
      where: {
        account,
      }
    })
    if (!!find) throw new ForbiddenError("账号已存在");
    const user = await this.userService.createUser({ account, password, name, isOrgan: false, province, city, email })
    return this.authService.getToken(user).access_token
  }

  /**
   * 企业注册
   */
  @Mutation(() => String)
  async organRegister(
    @Args("account") account: string,
    @Args("password") password: string,
    @Args("name") name: string,
    @Args("province", { nullable: true }) province?: string,
    @Args("city", { nullable: true }) city?: string,
    @Args("email", { nullable: true }) email?: string,
  ) {
    const find: SysUserEntity = await this.prisma.sys_user.findUnique({
      where: {
        account,
      }
    })
    if (!!find) throw new ForbiddenError("账号已存在");
    const user = await this.userService.createUser({ account, password, name, isOrgan: true, province, city, email })
    return this.authService.getToken(user).access_token
  }

  /**
   * 忘记密码
   */
  @Mutation(() => Boolean)
  async forgetPassword(

  ) {
    throw new Error("好好想想");
  }

}
